package com.baidu.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.baidu.mapper.OrderSettingMapper;
import com.baidu.pojo.OrderSetting;
import com.baidu.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

	@Autowired
	private OrderSettingMapper orderSettingMapper;

	@Override
	public void addOrder(ArrayList<OrderSetting> orderList) {

		for (OrderSetting orderSetting : orderList) {
			//		先查询该预约日期的，有没有重复的预约日期
			int count=	orderSettingMapper.findDate(orderSetting);

			if (count>0){
				//		查询出来有预约日期的，就再插入时跟新。
				orderSettingMapper.update(orderSetting);

			}else{
				//没有就直接插入
				orderSettingMapper.add(orderSetting);
			}

		}
	}

	@Override
	public List<Map> findOdersettingByMonth(String date) { //2018-01 >我们需要手动拼接天的日期。2018-01-01

		String date1=date+"-01";
		String date2=date+"-31"; //不能传递多个参数，所以要用map来封装.
		HashMap<String, String> map = new HashMap<>();
		map.put("begin",date1);
		map.put("end",date2);
		List<OrderSetting> list=  orderSettingMapper.findOdersettingByMonth(map);

		ArrayList<Map> list2 = new ArrayList<>();
		if (list.size()>0&&list!=null){
			Map<String, Object> map2 = null;
			for (OrderSetting orderSetting : list) {
				map2= new HashMap<>();
				//String day=String .format("%td", dt);
				map2.put("date",orderSetting.getOrderDate().getDate());
				map2.put("number",orderSetting.getNumber());
				map2.put("reservations",orderSetting.getReservations());

				list2.add(map2);

			}

		}



		return list2;
	}

	@Override
	public void updateOrderNumber(OrderSetting number) {

	 int count=orderSettingMapper.findDate(number);


	 if (count>0) {//表示有数据就更行
		 orderSettingMapper.updateOrderNumber(number);
	 }else{
	 	//就执行插入操作
		 orderSettingMapper.add(number);
	 }

	}
}
